﻿using JizCode.WPF;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSTMEETING.Win.App.Activities
{
    /// <summary>
    /// SignActivity.xaml 的交互逻辑
    /// </summary>
    public partial class SignActivity : Activity
    {
        public SignActivity()
        {
            InitializeComponent();
        }

        protected override void OnActive()
        {
            DSTMEETING.Win.App.Views.MainView.SetHeader("签名", new SignToolbar(this));
            if (IsFirstActive)
            {
            }
        }

        internal void Clear()
        {
            inkCanvas.Strokes.Clear();
        }

        internal void Submit()
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, PixelFormats.Default);
            rtb.Render(inkCanvas);

            string directory = string.Format("{0}signature\\", Config.Instance.StorageTemp);
            if (!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }
            string filepath = string.Format("{0}{1}.png", directory, Guid.NewGuid().ToString("N"));
            Debug.WriteLine(string.Format("write signature file:{0}", filepath));
            using (FileStream file = new FileStream(filepath, FileMode.CreateNew))
            {
                encoder.Frames.Add(BitmapFrame.Create(rtb));
                encoder.Save(file);
            }
            MessageBox.Show(string.Format("保存成功:{0}", filepath));
        }
    }
}
